﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Maticsoft.DBUtility;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem
{
    public partial class InsertRow : System.Web.UI.Page
    {
        public string TableName { get; set; }
        public string Year { get; set; }
        public string Month { get; set; }
        public string DWBM { get; set; }
        public string ZBBM { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            TableName = Request.Form["tn"].ToRequestString();
            Year = Request.Form["year"].ToRequestString();
            Month = Request.Form["month"].ToRequestString();
            DWBM = Request.Form["dwbm"].ToRequestString();
            Insert();
        }

        public void Insert()
        {
            StringBuilder sql = new StringBuilder();
            StringBuilder coluInfo = new StringBuilder();
            StringBuilder valueInfo = new StringBuilder();
            sql.Append("Insert into " + TableName + " (ZBBM,NF,YF,DWBM,CJ,QYJY,XSYC,SFKBJ,HC) Values (");
            sql.Append("'" + CreateNewChildPK(ZBBM) + "',");
            sql.Append("'" + Year + "',");
            sql.Append("'" + Month + "',");
            sql.Append("'" + DWBM + "',");
            sql.Append("'1',");
            sql.Append("'启用','显示','是',"+CreateNewHC(ZBBM)+"");
            sql.Append(")");
            DbHelperOra.ExecuteSql(sql.ToString());
        }

        public string CreateNewChildPK(string selectZBBM)
        {
            //Hack:编码规则:所选节点编码+000001=》理论上有99万条记录/月
            string sql = "select max(zbbm) as id from " + TableName + " where length(zbbm)=length('" + selectZBBM + "')+7 and ZBBM like '" + selectZBBM + "%' and NF='" + Year + "' and YF='" + Month + "'";
            object o = DbHelperOra.GetSingle(sql.ToString()); 
            if (o == null)
            {
                return selectZBBM + "-" + "000001";
            }
            else
            {
                int newid = Convert.ToInt16(o.ToString().Split('-').Last()) + 1;
                int zeroLenth = 6 - newid.ToString().Length;
                string zero = "";
                for (int i = 0; i < zeroLenth; i++)
                {
                    zero += "0";
                }
                zero += newid;
                return selectZBBM + "-" + zero;
            }
        }

        public int CreateNewHC(string selectZBBM)
        {
            //先计算插入的行次
            
            string sql = "select HC from " + TableName + " where NF='" + Year + "' and YF='" + Month + "' and DWBM='" + DWBM + "' and ZBBM = '" + selectZBBM + "'";
            int HC = Maticsoft.DBUtility.DbHelperOra.GetSingle(sql).ToInt32();
            int newHC = HC + 1;
            //update
            sql = "update " + TableName + " set HC=HC+1 where HC>" + newHC;
            Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql);
            return newHC;
        }
    }
}